package com.maoxx.libms.controller;


import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.extension.service.additional.AbstractChainWrapper;
import com.maoxx.libms.entity.Admin;
import com.maoxx.libms.service.IAdminService;
import com.maoxx.libms.vo.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;


/**
 * <p>
 *  前端控制器
 * </p>
 *
 * @author jobob
 * @since 2024-10-25
 */
@Controller
@RequestMapping("/admin")
public class AdminController {

    @Autowired
    private IAdminService adminService;

    @RequestMapping(path = "/login", name="登录页面")
    public String index() {
        return "page/login";
    }

    @PostMapping("/loginView")
    @ResponseBody
    public Result<Admin> login(@RequestBody Admin admin, HttpServletRequest request) {
        Admin resAdmin = adminService.getOne(new QueryWrapper<Admin>().eq("username", admin.getUsername()));
        if (resAdmin == null) {
            return Result.error("用户名不存在");
        }
        if (!resAdmin.getPassword().equals(admin.getPassword())) {
            return Result.error("密码错误");
        }
        request.getSession().setAttribute("admin", resAdmin.getId());
        return Result.success(resAdmin);
    }

    @PostMapping("/logout")
    @ResponseBody
    public Result<String> logout(HttpServletRequest request) {
        request.getSession().removeAttribute("admin");
        return Result.success("退出成功");
    }

}
